package com.example.utils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class ImageUtil {
    public static final Color redColor = new Color(255,65,92);

    public static boolean convertPngToJpg(File file, String newFilePath){
        try {

            BufferedImage bufferedImage = ImageIO.read(file);
            BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(),bufferedImage.getHeight(),BufferedImage.TYPE_INT_RGB);
            newBufferedImage.createGraphics().drawImage(bufferedImage, 0, 0, null);
            ImageIO.write(newBufferedImage,"jpg",new File(newFilePath));
        } catch (IOException e) {
//            e.printStackTrace();
            return false;
        }
        return true;
    }

    public static BufferedImage getBufferImage(File file){
        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bufferedImage;
    }
    public static BufferedImage getBufferImage(String file){
        BufferedImage bufferedImage = null;
        try {
            bufferedImage = ImageIO.read(new File(file));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bufferedImage;
    }
    //判断两个color是否相似
    public static boolean checkColorIsSimilar(Color color1,Color color2,int err){
        if(Math.abs(color1.getRed() - color2.getRed()) <= err && Math.abs(color1.getGreen() - color2.getGreen()) <= err && Math.abs(color1.getBlue() - color2.getBlue()) <= err){
            return true;
        }
        return false;
    }
}
